package com.kintreda.ieco_server.module.order.service.impl;

import com.kintreda.ieco_server.bean.massage.Massage;
import com.kintreda.ieco_server.bean.order.Draw;
import com.kintreda.ieco_server.bean.order.Wallte;
import com.kintreda.ieco_server.bean.user.Statistics;
import com.kintreda.ieco_server.core.service.impl.BaseServiceImpl;
import com.kintreda.ieco_server.module.account.service.IUserService;
import com.kintreda.ieco_server.module.api.service.IMassageService;
import com.kintreda.ieco_server.module.order.service.IDrawService;
import com.kintreda.ieco_server.module.order.service.IStatisticsService;
import com.kintreda.ieco_server.module.order.service.IWallteService;
import com.kintreda.ieco_server.module.organization.service.IOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.order.service.impl
 * @Author ZhongYao_H
 * @Date 2019/5/26:16:01
 * @Year 2019 Month 05 Day 26
 * @Date_zh 2019 年 五月 星期日
 */
@Service
public class WallteServiceImpl extends BaseServiceImpl<Wallte,String> implements IWallteService {

    @Autowired
    IUserService userService;
    @Autowired
    IStatisticsService statisticsService;
    @Autowired
    IOrganizationService organizationService;
    @Autowired
    IDrawService drawService;
    @Autowired
    IMassageService massageService;

    @Override
    public Boolean back(String wallteId,String msg) {
        List<Draw> draws = drawService.queryByWallteId(wallteId);
        for (Draw draw : draws) {
            BigDecimal money = draw.getMoney();
            Statistics statistics = statisticsService.get(draw.getStatisticsId());
            if (statistics!=null){
             BigDecimal   userBalance = statistics.getBalance();
                BigDecimal add = userBalance.add(money);
                statistics.setBalance(add);
                Massage massage = new Massage();
                massage.setDestination(statistics.getUserId());
                massage.setTitle("余额退回通知");
                massage.setBody(msg+",退回金额"+money.toString());
                massage.setStatus("0");
                statisticsService.update(statistics);
                massageService.save(massage);
            }

        }
        return true;
    }
}
